import { createRouter, createWebHistory } from 'vue-router'

import usersRoute from './userRoutes'
import store from '@/store/Store'
import adminRoute from './adminRoutes'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

export let router
//加载路由的方法
export default (bool)=>{
  const route = []
  if(bool){
    route.push(...adminRoute)
  }else{
    //此处暂时添加全部的用户中心路由，后续这里只能添加一些公共的路由
    route.push(...usersRoute)
  }
  route.push({
    // 404页面
    path: '/:pathMatch(.*)*',
    component: () => import ('@/views/notFound')
  })

  router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes:route,
  })

  router.beforeResolve((to, from,next) => {
    NProgress.start()
    next()
  })

  router.beforeEach((to,form,next)=>{
    !store.state.is_admin ? store.dispatch('userStore/removeHttpRequestList') : ''
    next()
  })

  router.afterEach(() => {
    NProgress.done()
  })
}
